#include "lcd_display.h"
#include "sin_cos.h"

char dispBuff[150];
struct lcd_manager g_lcd_manager = {
    .lcd_event = REFRESH_NONE,
    .cur_page = PAGE1,
    .charge_full_cnt = 0,
    .charge_decrement_cnt = 0,
};  //

struct lcd_display_info g_lcd_display_info = {
    .comm_state = ERR,
};  //
struct lcd_display_info g_lcd_display_info_last = {
    .comm_state = ERR,
};  //

uint8_t soc_flag = 0;

/* 显示背景 */
void _lcd_refresh_back(void)
{
    LCD_SetBackColor(BLACK);
    LCD_FillColor(0, 0, 240, 240, BLACK);

    /* 上方绿色框 */
    LCD_SetTextColor(GREEN);
    LCD_DrawRectangle(1, 1, 238, 110, 0);
    LCD_DrawRectangle(2, 2, 236, 108, 0);
    LCD_DrawRectangle(3, 3, 234, 106, 0);
    /* 下方白色SOC框 */
    LCD_SetTextColor(WHITE);
    LCD_DrawRectangle(3, 155, 228, 85, 0);
    LCD_DrawRectangle(4, 156, 226, 83, 0);
    LCD_DrawRectangle(5, 157, 224, 81, 0);
    LCD_DrawLine(231, 172, 231, 220);
    LCD_DrawLine(232, 171, 232, 221);
    LCD_DrawLine(233, 170, 233, 222);
    LCD_DrawLine(234, 169, 234, 223);

    LCD_SetFont(&Font16x32);
    LCD_DrawString_EN(15, 55, "0.0");
    LCD_DrawString_EN(130, 55, "0.0");

    LCD_SetFont(&Font12x24);
    LCD_DrawString_EN(40, 10, "Voltage");
    LCD_DrawString_EN(140, 10, "Current");
    LCD_SetFont(&Font12x24);
    LCD_DrawString_EN(95, 128, "SOC");
    LCD_SetTextColor(GREEN);
    LCD_DrawString_EN(95, 60, "V");
    LCD_SetTextColor(RED);
    LCD_DrawString_EN(205, 60, "A");

    LCD_SetBackColor(BLACK);
    LCD_SetTextColor(GREEN);
    LCD_DrawProgressBar(6, 158, 222, 79, 0);
    LCD_SetFont(&Font16x32);
    LCD_SetColors(WHITE, RED);
    LCD_DrawString_EN(100, 180, "0%");
}

/* 刷新数据 */
void _lcd_refresh_data()
{
    double pack_v = 0, pack_c = 0;
    static uint8_t last_event = 0;
    static uint16_t last_soc = 0;

    // g_lcd_display_info.pack_v = 1324;
    // g_lcd_display_info.pack_c = -4059;
    // g_lcd_display_info.soc = 10;
    // g_lcd_display_info.soc_status = CHARGING;

    LCD_SetBackColor(BLACK);
    if (last_event == g_lcd_manager.lcd_event)
    {
        LCD_FillColor(10, 55, 220, 30, BLACK);
        LCD_FillColor(10, 128, 220, 26, BLACK);
    }
    else
    {
        LCD_FillColor(0, 0, 240, 240, BLACK);
    }
    
    /* 上方绿色框 */
    LCD_SetTextColor(GREEN);
    LCD_DrawRectangle(1, 1, 238, 110, 0);
    LCD_DrawRectangle(2, 2, 236, 108, 0);
    LCD_DrawRectangle(3, 3, 234, 106, 0);
    /* 下方白色SOC框 */
    LCD_SetTextColor(WHITE);
    LCD_DrawRectangle(3, 155, 228, 85, 0);
    LCD_DrawRectangle(4, 156, 226, 83, 0);
    LCD_DrawRectangle(5, 157, 224, 81, 0);
    LCD_DrawLine(231, 172, 231, 220);
    LCD_DrawLine(232, 171, 232, 221);
    LCD_DrawLine(233, 170, 233, 222);
    LCD_DrawLine(234, 169, 234, 223);

    LCD_SetFont(&Font16x32);
    pack_v = (double)g_lcd_display_info.pack_v / 100;
    pack_c = (double)g_lcd_display_info.pack_c / 100;
    sprintf(dispBuff,"%03.1lf", pack_v);
    LCD_DrawString_EN(25, 55, dispBuff);
    sprintf(dispBuff,"%03.1lf", pack_c);
    if (pack_c > 0)
    {
        if (pack_c >= 10)
        {
            LCD_DrawString_EN(135, 55, dispBuff);
        }
        else
        {
            LCD_DrawString_EN(150, 55, dispBuff);
        }
    }
    else
    {
        if (pack_c <= -10)
        {
            LCD_DrawString_EN(120, 55, dispBuff);
        }
        else
        {
            LCD_DrawString_EN(135, 55, dispBuff);
        }
    }
 
 
    LCD_SetFont(&Font12x24);
    LCD_DrawString_EN(40, 10, "Voltage");
    LCD_DrawString_EN(140, 10, "Current");
    LCD_SetFont(&Font12x24);    
    LCD_SetTextColor(GREEN);
    LCD_DrawString_EN(95, 60, "V");
    LCD_SetTextColor(RED);
    LCD_DrawString_EN(205, 60, "A");

    LCD_SetTextColor(WHITE);
    if (g_lcd_display_info.soc_status == CHARGING)
    {
        if (g_lcd_display_info.soc == 100)
        {
            LCD_DrawString_EN(95, 128, "SOC");
        }
        else
        {
            LCD_SetTextColor(GREEN);
            LCD_DrawString_EN(55, 128, "CHARGING");
        }
    }
    else
    {
        if (g_lcd_display_info.soc >= 20)
        {
            LCD_SetTextColor(WHITE);
            LCD_DrawString_EN(95, 128, "SOC");
        }
        else
        {
            LCD_SetTextColor(YELLOW);
            LCD_DrawString_EN(35, 128, "LOW BATTERY");
        }
    }
    
    LCD_SetBackColor(BLACK);
    if (g_lcd_display_info.soc < 20)
    {
        LCD_SetTextColor(RED);
    }
    else if (g_lcd_display_info.soc < 50)
    {
        LCD_SetTextColor(YELLOW);
    }
    else
    {
        LCD_SetTextColor(GREEN);
    }
    
    if (last_soc != g_lcd_display_info.soc)
    {
        LCD_DrawProgressBar(6, 158, 222, 79, g_lcd_display_info.soc);
    }

    LCD_SetFont(&Font16x32);
    LCD_SetTextColor(WHITE);
    sprintf(dispBuff,"%3d%c", g_lcd_display_info.soc, '%');
    LCD_DrawString_EN(85, 180, dispBuff);
    last_event = g_lcd_manager.lcd_event;
    last_soc = g_lcd_display_info.soc;
}

/****
 * 
 * LCD任务
*/
void lcd_task(void)
{
#if 1
    if (g_lcd_manager.lcd_event == REFRESH_NONE)
    {
        return;
    }

    usart_interrupt_disable(USART0, USART_INT_RBNE);

    switch (g_lcd_manager.lcd_event)
    {
    case REFRESH_BACK:
        _lcd_refresh_back();
        break;
    
    case REFRESH_DATA:
		_lcd_refresh_data();
        break;

    default:
        break;
    }
    g_lcd_manager.lcd_event = REFRESH_NONE;

	usart_interrupt_enable(USART0, USART_INT_RBNE);
#endif

#if 0
    double pack_v = 0, pack_c = 0;
    static uint8_t last_event = 0;
    static uint16_t last_soc = 0;

    LCD_SetBackColor(BLACK);

    /* 上方绿色框 */
    LCD_SetTextColor(GREEN);
    LCD_DrawRectangle(2, 2, 236, 108, 0);
    LCD_DrawRectangle(3, 3, 234, 106, 0);
    /* 下方白色SOC框 */
    LCD_SetTextColor(WHITE);
    LCD_DrawRectangle(4, 156, 226, 83, 0);
    LCD_DrawRectangle(5, 157, 224, 81, 0);
    LCD_DrawLine(231, 172, 231, 220);
    LCD_DrawLine(232, 171, 232, 221);
    LCD_DrawLine(233, 170, 233, 222);
    LCD_DrawLine(234, 169, 234, 223);

    LCD_SetFont(&Font12x24);
    LCD_DrawString_EN(20, 13, "Voltage");
    LCD_DrawString_EN(140, 13, "Current");
    LCD_SetFont(&Font12x24);
    LCD_DrawString_EN(100, 128, "SOC");

    LCD_SetFont(&Font12x24);
    LCD_SetTextColor(GREEN);
    LCD_DrawString_EN(100, 60, "V");
    LCD_SetTextColor(RED);
    LCD_DrawString_EN(220, 60, "A");

    if (g_lcd_display_info.comm_state == UART0) // 485有通讯
    {
        last_event = 0;
        LCD_SetFont(&Font16x32);
        pack_v = (double)g_lcd_display_info.pack_v / 100;
        pack_c = (double)g_lcd_display_info.pack_c / 100;
        sprintf(dispBuff,"%5.1lf", pack_v);
        LCD_DrawString_EN(20, 50, dispBuff);
        sprintf(dispBuff,"%5.1lf", pack_c);
        LCD_DrawString_EN(138, 50, dispBuff);
        // if (pack_c > 0)
        // {
        //     if (pack_c >= 10)
        //     {
        //         LCD_DrawString_EN(135, 55, dispBuff);
        //     }
        //     else
        //     {
        //         LCD_DrawString_EN(150, 55, dispBuff);
        //     }
        // }
        // else
        // {
        //     if (pack_c <= -10)
        //     {
        //         LCD_DrawString_EN(120, 55, dispBuff);
        //     }
        //     else
        //     {
        //         LCD_DrawString_EN(135, 55, dispBuff);
        //     }
        // }

        LCD_SetTextColor(WHITE);
        LCD_SetFont(&Font12x24);
        if (g_lcd_display_info.soc_status == CHARGING)
        {
            if (g_lcd_display_info.soc == 100)
            {
                LCD_DrawString_EN(100, 128, "SOC");
            }
            else
            {
                LCD_SetTextColor(GREEN);
                LCD_DrawString_EN(72, 128, "CHARGING");
            }
        }
        else
        {
            if (g_lcd_display_info.soc >= 20)
            {
                LCD_SetTextColor(WHITE);
                LCD_DrawString_EN(100, 128, "SOC");
            }
            else
            {
                LCD_SetTextColor(YELLOW);
                LCD_DrawString_EN(60, 128, "LOW BATTERY");
            }
        }
        
        LCD_SetBackColor(BLACK);
        if (g_lcd_display_info.soc < 20)
        {
            LCD_SetTextColor(RED);
        }
        else if (g_lcd_display_info.soc < 50)
        {
            LCD_SetTextColor(YELLOW);
        }
        else
        {
            LCD_SetTextColor(GREEN);
        }
        
        if (last_soc != g_lcd_display_info.soc)
        {
            LCD_DrawProgressBar(7, 159, 220, 77, g_lcd_display_info.soc);
        }

        LCD_SetFont(&Font16x32);
        LCD_SetTextColor(WHITE);
        sprintf(dispBuff,"%3d%c", g_lcd_display_info.soc, '%');
        LCD_DrawString_EN(90, 180, dispBuff);
        last_soc = g_lcd_display_info.soc;
    }
    else if (g_lcd_display_info.comm_state == NONE) // 无通讯
    {
        if (last_event == 0x55)
        {
            return;
        }
        
        LCD_SetFont(&Font16x32);
        LCD_SetTextColor(WHITE);
        LCD_DrawString_EN(20, 50, "  0.0");
        LCD_DrawString_EN(138, 50, "  0.0");

        LCD_SetBackColor(BLACK);
        LCD_SetTextColor(GREEN);
        LCD_DrawProgressBar(7, 159, 220, 77, 0);    // SOC进度条
        LCD_SetFont(&Font16x32);
        LCD_SetTextColor(WHITE);
        LCD_DrawString_EN(105, 180, "0%");
        last_event = 0x55;
    }
#endif

}
